package com.svo.weitu.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.svo.platform.picbrowser.PicBrowseActivity;
import com.svo.platform.utils.HttpUtil;
import com.svo.platform.utils.PicUtil;
import com.svo.platform.utils.SvoToast;
import com.svo.platform.widget.QuickAction;
import com.svo.platform.widget.QuickActionGrid;
import com.svo.platform.widget.QuickActionWidget;
import com.svo.platform.widget.QuickActionWidget.OnQuickActionClickListener;
import com.svo.weitu.R;
import com.svo.weitu.model.MCol;
import com.svo.weitu.model.db.ListData;
import com.svo.weitu.model.entity.ListEntity;
import com.svo.weitu.util.Cons;
import com.svo.weitu.util.MyUtil;
import com.svo.weitu.util.UiUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class PicBrowser extends PicBrowseActivity {
	private ImageView loveImg;
	private int infoId;
	private ListEntity entity;
	private TextView descTv;

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		infoId = getIntent().getIntExtra("id", 0);
		entity = (ListEntity) getIntent().getSerializableExtra("entity");

		loveImg = (ImageView) findViewById(R.id.love);
		descTv = (TextView) findViewById(R.id.desc_tv);
		shareImg.setOnClickListener(shareListener);
		descTv();
		loveImg.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (MyUtil.isNetworkAvailable(PicBrowser.this)) {
					loveImg.setImageResource(R.drawable.like_zan);
					if (infoId > 0){
						HttpUtil.get(Cons.gen+"list/love?id="+infoId, null, new AsyncHttpResponseHandler(){
							public void onSuccess(String arg0) {
								new Thread(){
									public void run() {
										new ListData(PicBrowser.this).updateLoves(infoId);
									};
								}.start();
							};
						});
					}
				}
				if (entity != null) {
					new MCol(PicBrowser.this).insert(entity);
					SvoToast.showHint(getApplicationContext(), "已添加到收藏", Toast.LENGTH_SHORT);
				}
			}
		});
		findViewById(R.id.wallpaper).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				QuickActionGrid mGrid = new QuickActionGrid(PicBrowser.this);
				ArrayList<HashMap<String, Object>> data = getAdapterValues();
				for (int i = 0; i < data.size(); i++) {
					HashMap<String, Object> map = data.get(i);
					mGrid.addQuickAction(new QuickAction(PicBrowser.this, (Integer)map.get("img"), (String)map.get("ItemTitle")));
				}
		        mGrid.show(view);
		        mGrid.setOnQuickActionClickListener(new OnQuickActionClickListener() {
					
					@Override
					public void onQuickActionClicked(QuickActionWidget widget, int position) {
						widget.dismiss();
						try {
							String picPath = PicUtil.getPicFile(picsPath.get(curPosition)).getAbsolutePath();
							if (position == 0) { // 设置壁纸
								Bitmap bitmap = BitmapFactory.decodeFile(picPath);
								setWallpaper(bitmap);
								SvoToast.showHint(PicBrowser.this, "壁纸设置成功", Toast.LENGTH_LONG);
							} else { // 戳一戳
								Intent intent = new Intent(PicBrowser.this, NieActivity.class);
								intent.putExtra("from", "PicBrowser");
								intent.putExtra("picPath", picPath);
								startActivity(intent);
								overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
							}
						} catch (Exception e) {
							e.printStackTrace();
							SvoToast.showHint(PicBrowser.this, "操作失败", Toast.LENGTH_LONG);
						}
					}
				});
			}
		});
		int random = Math.abs(new Random().nextInt());
		if (random % 5 == 0) {
			SvoToast.showHint(this, "好东西要分享哦", Toast.LENGTH_LONG);
		}
	}

	private void descTv() {
		if (!TextUtils.isEmpty(subContent)) {
			descTv.setText(Html.fromHtml(subContent));
			descTv.setVisibility(View.VISIBLE);
		}else {
			descTv.setVisibility(View.GONE);
			if (title != null) {
				descTv.setText(title);
				descTv.setVisibility(View.VISIBLE);
			}
		}
	}

	private ArrayList<HashMap<String, Object>> getAdapterValues() {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> map = new HashMap<>();
        map.put("ItemTitle", "设为壁纸");
        map.put("img", R.drawable.bi);
        data.add(map);
        map = new HashMap<>();
        map.put("ItemTitle", "戳一戳");
        map.put("img", R.drawable.cuo);
        data.add(map);
		return data;
	}

	@Override
	protected void changeDesc(int position) {
		super.changeDesc(position);
		if (picInfos != null && picInfos.size() > position) {
			subContent = picInfos.get(position);
			descTv();
		}
	}

	OnClickListener shareListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			String wxText = subContent;
			if (TextUtils.isEmpty(subContent)) {
				wxText = title;
			}
			wxText = wxText + " "+Cons.WEITU_SITE;
			new ShareAction(PicBrowser.this)
			.withText(wxText)
		    .withMedia(new UMImage(PicBrowser.this, PicUtil.getPicFile(picsPath.get(curPosition))))                    
		    .setDisplayList(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN_FAVORITE)
		    .setCallback(new UMShareListener() {
				
				@Override
				public void onStart(SHARE_MEDIA arg0) {
					UiUtil.toast(getApplicationContext(),"分享开始");
				}
				
				@Override
				public void onResult(SHARE_MEDIA arg0) {
					UiUtil.toast(getApplicationContext(),"分享成功");
				}
				
				@Override
				public void onError(SHARE_MEDIA arg0, Throwable arg1) {
					UiUtil.toast(getApplicationContext(),"分享出错");
				}
				
				@Override
				public void onCancel(SHARE_MEDIA arg0) {
					UiUtil.toast(getApplicationContext(),"分享取消");
				}
			})
		    .open();
		}
	};
	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
	}
}
